package lab1;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class T3_DateProblem {
    //设置日期
    public Calendar setDate(int[] date) {
        Calendar c = Calendar.getInstance();
        c.set(date[0], date[1], date[2]);
        return c;
    }

    //获取后天的日期
    public Calendar handle(Calendar c) {
        c.add(Calendar.DATE, 2);
        return c;
    }

    //输出日期
    public void printDate(Calendar c) {
        Date date = c.getTime();
        SimpleDateFormat formater = new SimpleDateFormat("yyyy年MM月dd日");
        System.out.println(formater.format(date));
    }

    /**
     * 测试入口
     */
    int[] testing_date;
    @Test
    public void testDateProblem() {
        ArrayList<int[]> dateList = new ArrayList<>();
        dateList.add(new int[]{2001, 6, 16});
        dateList.add(new int[]{2001, 6, 30});
        dateList.add(new int[]{2001, 6, 31});
        dateList.add(new int[]{2001, 1, 16});
        dateList.add(new int[]{2001, 1, 31});
        dateList.add(new int[]{2001, 12, 16});
        dateList.add(new int[]{2001, 12, 31});
        dateList.add(new int[]{2001, 2, 16});
        dateList.add(new int[]{2004, 2, 28});
        dateList.add(new int[]{2001, 2, 28});
        dateList.add(new int[]{2004, 2, 29});
        dateList.add(new int[]{2001, 2, 29});
        dateList.add(new int[]{2001, 2, 30});
        for (int[] date : dateList) {
            testing_date = date;
            testDate();
        }
    }

    public void testDate() {
        String date_String = testing_date[0] + "年" + testing_date[1] + "月" + testing_date[2] + "日";
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd");
        //利用SimpleDateFormat类验证日期合法性
        try {
            //设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(date_String);
        } catch (ParseException e) {
            System.out.println("您输入的'" + date_String + "'不是一个合法的日期");
            return;
        }
        Calendar c = setDate(testing_date);
        c = handle(c);
        printDate(c);
    }
}
